import 'package:flutter/material.dart';
import 'package:client/common/conn.dart';
import 'package:client/common/event.dart';
import 'package:client/common/fun.dart';
import 'package:client/provider/app_provider.dart';
import 'package:client/view/home.dart';
import 'package:client/view/login.dart';
import 'package:provider/provider.dart';

class MainApp extends StatelessWidget {
  const MainApp({super.key});

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
        title: 'Flutter Demo',
        theme: ThemeData(
          colorScheme: ColorScheme.fromSeed(seedColor: Colors.deepPurple),
          useMaterial3: true,
        ),
        home: FirstRoute());
  }
}

class FirstRoute extends StatelessWidget {
  const FirstRoute({super.key});

  //初始化
  init(BuildContext context) {
    AppProvider.init(context);

    // bus.on(EventBusSet.sound, (arg) {
    //   Fun.sound();
    // });
  }

  @override
  Widget build(BuildContext context) {
    //初始化
    init(context);

    var view = Consumer<AppProvider>(
      builder: (context, app, child) {
        print("app.isLogin: ${app.isLogin}");

        if (!app.isLogin) {
          return LoginRoute();
        } else {
          return HomeRoute();
        }
      },
    );

    return view;
  }
}
